import {createRouter, createWebHistory} from 'vue-router';
import Layout from "@/layout/index.vue";
import {createRouterGuards} from '@/permission';

export const constantRoutes = [
    {
        path: '/redirect',
        component: Layout,
        hidden: true,
        children: [
            {
                path: '/redirect/:path(.*)',
                component: () => import('@/views/redirect/index.vue')
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        hidden: true,
        component: () => import('@/views/login/index.vue')
    },
    {
        path: '',
        component: Layout,
        name: 'layout',
        redirect: 'index',
        children: [
            {
                path: '/index',
                component: () => import('@/views/dashboard/index.vue'),
                name: 'Index',
                meta: { title: '工作台', icon: 'dashboard', affix: true }
            }
        ]
    },
    // {
    //     path: '/user',
    //     component: Layout,
    //     hidden: true,
    //     redirect: 'redirect',
    //     children: [
    //         {
    //             path: 'profile',
    //             component: () => import('@/views/system/profile/index.vue'),
    //             name: 'Profile',
    //             meta: { title: '个人中心', icon: 'user' }
    //         }
    //     ]
    // },
    {
        path: '/404',
        name: '404',
        component: () => import('@/views/error/404.vue'),
        hidden: true
    }
    // {
    //   path: '/:pathMatch(.*)*',
    //   component: () => import('@/views/error/404.vue'),
    //   hidden: true
    // }
];

const router = createRouter({
    history: createWebHistory(),
    routes: constantRoutes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        } else {
            return { top: 0 };
        }
    }
});

export async function setupRouter(app) {
    // 创建路由守卫
    createRouterGuards(router);
    app.use(router);
    // 路由准备就绪后挂载APP实例
    await router.isReady();
}

export default router;
